﻿Imports WIA
Imports System.Drawing


''' <summary>
''' This class manage the device scanner.
''' </summary>
''' <auteur>Adama MBODJI</auteur>
''' <Compagny>MBODJ.SYSTEM</Compagny>
''' <Date>24/04/2011</Date>
''' <remarks>
''' Add the com references "c:\windows\system32\wiaaut.dll" in your project.
''' Please, if you have some questions, contact me at adama@mbodjsystem.sn
''' </remarks>
''' <website>http://www.mbodjsystem.sn</website>
''' 
Public NotInheritable Class Scanner

#Region "Variables"
    Private Shared m_DefaultImageFolder As String = My.Computer.FileSystem.SpecialDirectories.MyPictures
#End Region

#Region "Propriétés"


    Public Shared Property DefaultImageFolder As String
        Get
            If Not m_DefaultImageFolder.EndsWith("\") Then
                m_DefaultImageFolder += "\"
            End If
            Return m_DefaultImageFolder
        End Get
        Set(value As String)
            If Not value.EndsWith("\") Then
                value += "\"
            End If

            If Not IO.Directory.Exists(value) Then
                Throw New Exception("Directory not exist")
            End If

            m_DefaultImageFolder = value
        End Set
    End Property
#End Region

#Region "Fonctions scanner"
    ''' <summary>
    ''' Lance le scanner et renvoie une image de type System.Drawing.Image
    ''' </summary>
    ''' <returns>System.Drawing.Image</returns>
    ''' <remarks></remarks>
    Public Shared Function Scan(quality As ScannerQuality, fileType As FileExtension) As System.Drawing.Image

        Dim imageFile As New ImageFile
        Dim dialog As New WIA.CommonDialog

        Dim formatID As String = String.Empty
        Dim extension As String = String.Empty

        Select Case fileType

            Case FileExtension.Bmp
                formatID = WIA.FormatID.wiaFormatBMP
                extension = ".bmp"

            Case FileExtension.Gif
                formatID = WIA.FormatID.wiaFormatGIF
                extension = ".gif"

            Case FileExtension.Jpeg
                formatID = WIA.FormatID.wiaFormatJPEG
                extension = ".jpeg"

            Case FileExtension.Png
                formatID = WIA.FormatID.wiaFormatPNG
                extension = ".png"

            Case FileExtension.Tiff
                formatID = WIA.FormatID.wiaFormatTIFF
                extension = ".tiff"

        End Select

        Try

            If quality = ScannerQuality.Best Then

                imageFile = dialog.ShowAcquireImage(WiaDeviceType.ScannerDeviceType,
                                                    WiaImageIntent.ColorIntent,
                                                    WiaImageBias.MaximizeQuality,
                                                    formatID,
                                                    True,
                                                    True)

            Else

                imageFile = dialog.ShowAcquireImage(WiaDeviceType.ScannerDeviceType,
                                                  WiaImageIntent.ColorIntent,
                                                  WiaImageBias.MinimizeSize,
                                                  formatID,
                                                  True,
                                                  True)

            End If
        Catch ex As Exception
            Select Case WiaErrCode(ex.Message)

                Case ScannerStatut.NoScannerDeviceDetected
                    Throw New ExceptionHandling.ScannerNoDeviceDetectedException

                Case ScannerStatut.CancelPressed
                    Throw New ExceptionHandling.ScannerCancelPressedException

                Case ScannerStatut.ScannerNotReady
                    Throw New ExceptionHandling.ScannerNotReadyException

                Case Else
                    Throw New Exception("ERROR_NOT_DEFINE")

            End Select

        End Try

        Dim fileName As String = String.Format("MS_Scan{0}{1}{2}{3}{4}", Now.Day, Now.Hour, Now.Minute, Now.Second, extension)

        Dim monFileInfo As New IO.FileInfo(DefaultImageFolder + fileName)
        imageFile.SaveFile(monFileInfo.FullName)

        imageFile = Nothing

        Dim scannedImage As Image = Image.FromFile(monFileInfo.FullName)

        Return scannedImage

    End Function

#End Region

#Region "Fonctions internes"

    Private Shared Function WiaErrCode(Optional ByVal exMessage As String = "") As Integer

        Dim errNumber As String = String.Empty

        Dim posFound As Integer = InStr(exMessage, ":")

        If posFound <> 0 Then
            errNumber = Mid(exMessage, posFound + 2)
        End If

        Select Case errNumber

            Case "0x80210015"               '// No device detected.
                Return ScannerStatut.NoScannerDeviceDetected

            Case "0x80210064"               '// Cancel was pressed
                Return ScannerStatut.CancelPressed

            Case "0x80210003"
                Return ScannerStatut.ScannerNotReady

            Case Else
                Return ScannerStatut.NotDefine

        End Select

    End Function

#End Region

End Class

